import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:short_video/entity/home/comment_list_entity.dart';
import 'package:short_video/utils/date_util.dart';

import '../../../entity/home/video_list_entity.dart';
import '../../../utils/color_util.dart';
import '../../../view_model/home/comment_content_view_model.dart';
import '../../widgets/custom_network_image.dart';
import '../../widgets/custom_refresh.dart';
import '../../widgets/empty_page.dart';

class CommentContent extends StatefulWidget {
  final VideoEntity? entity;
  const CommentContent({Key? key, this.entity}) : super(key: key);

  @override
  State<CommentContent> createState() => _CommentContentState();
}

class _CommentContentState extends State<CommentContent> {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (BuildContext context) => CommentContentViewModel(e: widget.entity),
      child: Consumer<CommentContentViewModel>(
          builder: (context, commentViewModel, child) {
        return Scaffold(
          body: CustomRefresh(
            controller: commentViewModel.refreshController,
            enablePullUp: true,
            onRefresh: () async {
              commentViewModel.onRefresh();
            },
            onLoading: () async {
              commentViewModel.onLoading();
            },
            child: _buildContent(commentViewModel),
          ),
        );
      }),
    );
  }

  Widget _buildContent(CommentContentViewModel viewModel) {
    return viewModel.datas.isEmpty
        ? const EmptyPage()
        : ListView.builder(
            // shrinkWrap: true,
            itemCount: viewModel.datas.length,
            padding: EdgeInsets.zero,
            primary: true,
            // physics: NeverScrollableScrollPhysics(),
            itemBuilder: (BuildContext context, int index) {
              return _buildItem(context, viewModel, index);
            });
  }

  Widget _buildItem(
      BuildContext context, CommentContentViewModel viewModel, int index) {
    CommentEntity cEntity = viewModel.datas[index];
    return GestureDetector(
      child: Container(
        padding: const EdgeInsets.only(left: 16, right: 16),
        child: Column(
          children: [
            Container(
              padding: const EdgeInsets.only(top: 16, bottom: 16),
              child: Row(
                children: [
                  ClipRRect(
                    borderRadius: BorderRadius.circular(20),
                    child: Container(
                      color: ColorsUtil.COLOR_DBDBDB,
                      width: 40,
                      height: 40,
                      child: const CustomNetworkImage(
                        url:
                            "https://img1.baidu.com/it/u=1773366646,898438994&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=889",
                        fit: BoxFit.cover,
                        logoSize: Size(22, 22),
                      ),
                    ),
                  ),
                  const SizedBox(width: 16),
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          cEntity.nickName ?? "",
                          style: const TextStyle(
                            fontSize: 14,
                            color: ColorsUtil.COLOR_333333,
                          ),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                        Text(
                          cEntity.content ?? "",
                          style: const TextStyle(
                            fontSize: 14,
                            color: ColorsUtil.COLOR_333333,
                          ),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                        Text(
                          DateUtil.yearMonthDayHourMinuteDateFormat(cEntity.createTime ?? 0),
                          style: const TextStyle(
                            fontSize: 14,
                            color: ColorsUtil.COLOR_999999,
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            index != viewModel.datas.length - 1
                ? Container(height: 0.5, color: ColorsUtil.COLOR_EBEBEB)
                : Container(),
          ],
        ),
      ),
      behavior: HitTestBehavior.opaque,
      onTap: () {},
    );
  }
}
